#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Core/ADO/goodinfodao.h"
#include "iostream"
#include "QAction"
#include "QListWidgetItem"
#include "QDebug"
#include <QMessageBox>
#include "UiScipt/aboutwidget.h"
#include "UiScipt/exportdatawidget.h"
#include "UiScipt/importdatawidget.h"
#include "UiScipt/settingpwsswdwidget.h"
#include "Core/ADO/dbsearchhelper.h"
#include "QPixmap"
#include "QFile"
#include "UiScipt/goodinfoeditor.h"




MainWindow* MainWindow::instance = NULL;

MainWindow *MainWindow::GetInstance()
{
    return instance;
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    IMG_DIR_PATH(QString("%1/GoodImages").arg(QApplication::applicationDirPath())),
    m_cur_select_good_info(NULL)
{
    ui->setupUi(this);
    instance = this;
    // 尝试开启密码验证
    auto pw = SettingPwsswdWidget::TryNewSettingPwsswdWidget(ValidatePW);
    if(pw != NULL)
    {
        pw->exec();
    }
    else
    {
        delete pw;
        pw = NULL;
    }
    this->ui->good_item_list->clear();
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 刷新列表
void MainWindow::SetSearchIdInputValue(std::vector<QString> *str_list)
{
    this->ui->good_item_list->clear();
    for(auto it = str_list->begin(); it != str_list->end(); it++)
    {
        //qDebug() << *it;
        this->ui->good_item_list->addItem(*it);
    }
}

// 输入框搜索栏值改变事件
void MainWindow::on_search_id_input_textChanged(const QString &val, bool is_refresh)
{
    m_cur_select_good_info = NULL;
    QString search_id = this->ui->search_id_input->text();
    if(is_refresh)
    {
        DBSearchHelper::GetInstance()->RefreshSearch(search_id);
    }
    else
    {
        DBSearchHelper::GetInstance()->SearchGoodInfo(search_id);
    }
    auto str_list = DBSearchHelper::GetInstance()->GetSearchList();
    SetSearchIdInputValue(str_list);
}

// 点击列表item事件
void MainWindow::on_good_item_list_clicked(const QModelIndex &index)
{
    int row = index.row();
    //qDebug()<<"____on_good_item_list_clicked   " << row ;
    try
    {
       auto item = ui->good_item_list->item(row);
       auto good_id = item->text();
       auto good_info = DBSearchHelper::GetInstance()->GetGoodInfo(good_id);
       ShowGoodInfo(good_info);
       m_cur_select_good_info = good_info;
    }
    catch(std::exception e)
    {
        qDebug() << e.what() ;
    }
}


// 显示信息
void MainWindow::ShowGoodInfo(GoodInfo *good_info)
{
    ui->name_lab_in->setText(good_info->good_name);
    ui->id_lab_in->setText(good_info->good_id);
    ui->cost_price_lab_in->setText(QString::number(good_info->cost_price));
    ui->sale_price_lab_in->setText(QString::number(good_info->sale_price));
    ui->inventory_lab_in->setText(QString::number(good_info->inventory));
    ui->dis_lab_in->setText(good_info->description);

    // 设置图像
    QPixmap image(ui->show_icon_lab_in->width(),ui->show_icon_lab_in->height());
    QString img_path = good_info->img_name;

    QFile get_img_file(img_path);
    if(get_img_file.exists())
    {
       image.load(img_path);
    }
    else
    {
        img_path = QString(":/images/CheckFail.jpg");
        image.load(img_path);
    }

    ui->show_icon_lab_in->clear();
    ui->show_icon_lab_in->setPixmap(image);
}

/********************点击事件begin**********************/

// 退出
void MainWindow::on_quit_action_triggered()
{
    QMessageBox box(QMessageBox::Warning, "关闭" , "你是否确定要关闭系统" );
    box.setStandardButtons (QMessageBox::Ok|QMessageBox::Cancel);
    box.setButtonText(QMessageBox::Ok,QString("确定"));
    box.setButtonText(QMessageBox::Cancel,QString("取消"));
    int val = box.exec();
    if(val == QMessageBox::Ok)
    {
        qDebug() << QString("YES  %1").arg(val);
        this->close();
        //Application
    }
    else
    {
        qDebug() <<QString("No    %1").arg(val);
    }
}

// 设置密码
void MainWindow::on_pw_action_triggered()
{
    auto pw = SettingPwsswdWidget::TryNewSettingPwsswdWidget(ChangePW);
    if(pw != NULL)
    {
        pw->exec();
    }
}

// 导入
void MainWindow::on_inport_action_triggered()
{
    auto import_widget = new ImportDataWidget();
    import_widget->exec();
    on_search_id_input_textChanged(QString(""),true);
}

// 导出
void MainWindow::on_export_action_triggered()
{
    auto export_widget = new ExportDataWidget();
    export_widget->exec();
}

// 关于
void MainWindow::on_about_action_triggered()
{
    auto about_widget = new AboutWidget();
    about_widget->exec();
}

// 资料编辑-修改
void MainWindow::on_change_info_btn_clicked()
{
    if(m_cur_select_good_info == NULL)
    {
        qDebug() << "  m_cur_select_good_info  == NULL " ;
        return;
    }
   auto editor = new GoodInfoEditor(ChangeInfo,m_cur_select_good_info);
   editor->exec();

   // 修改过后会修改数据库里的信息，需要重新刷新一次才能够保证数据的正确性
   on_search_id_input_textChanged(QString(""),true);
}

// 修改资料-添加
void MainWindow::on_add_info_btn_clicked()
{
   auto editor = new GoodInfoEditor(AddInfo);
   editor->exec();
   on_search_id_input_textChanged(QString(""),true);
}

// 修改资料-删除
void MainWindow::on_delete_info_btn_clicked()
{
    if(m_cur_select_good_info != NULL)
    {
        QMessageBox box(QMessageBox::Warning,"删除","确定要删除吗");
        box.setStandardButtons (QMessageBox::Ok|QMessageBox::Cancel);
        box.setButtonText(QMessageBox::Ok,QString("确定"));
        box.setButtonText(QMessageBox::Cancel,QString("取消"));
        int val = box.exec();
        if(val == QMessageBox::Ok)
        {
            qDebug() << QString("Delete  %1").arg(QString::number(val));
            m_cur_select_good_info->PrintData("__Delete");
            GoodInfoDao::GetInstance()->Delete(*m_cur_select_good_info);
            m_cur_select_good_info = NULL;
            on_search_id_input_textChanged(QString(""),true);
        }
        else
        {
            qDebug() <<QString("No    %1").arg(val);
        }
    }
}
/********************点击事件end**********************/




//// Insert
//void MainWindow::on_pushButton_clicked()
//{

//    std::vector<GoodInfo> info_list;
//    for(int i = 1 ;i <= 1000 ; ++i )
//    {
//        GoodInfo info;

//        auto str = QString::number(i + 1000);
//        info.good_id = i <= 10 ? QString("%1").arg(QString::number(i * 10)) : QString("id_%1").arg(str);
//        info.good_name = QString("name_%1").arg(str);
//        info.img_name = QString("img_%1.jpg").arg(str);
//        info.description = str;
//        info.cost_price = i + 10000.1;
//        info.sale_price = i * 10 + 1234.5;
//        info.inventory = i;

//        info_list.push_back(info);
//    }
//    GoodInfoDao::GetInstance()->Insert(info_list);
//}

//// Select
//void MainWindow::on_pushButton_2_clicked()
//{
//   std::vector<GoodInfo> infos = GoodInfoDao::GetInstance()->Select("id_10");
//   for(GoodInfo info : infos)
//   {
//       info.PrintData("_Select(\"id_10055\")");
//   }

//   infos.clear();
//   infos = GoodInfoDao::GetInstance()->Select("id_1005");
//   for(GoodInfo info : infos)
//   {
//       info.PrintData("_Select(\"id_1005\")");
//   }
//}

//// Update
//void MainWindow::on_pushButton_3_clicked()
//{
//    std::vector<GoodInfo> infos = GoodInfoDao::GetInstance()->Select("id_10055");
//    GoodInfo info = infos[0];
//    info.PrintData("__Update before");

//    info.good_name = "change_after";
//    info.cost_price = 1314.52;
//    info.sale_price = 52013.14;

//    info.PrintData("__Change");

//    GoodInfoDao::GetInstance()->Update(info);
//    infos = GoodInfoDao::GetInstance()->Select("id_10055");
//    info = infos[0];
//    info.PrintData("__Update after");

//}

//// ReadFullTalbe
//void MainWindow::on_pushButton_4_clicked()
//{
//    std::vector<GoodInfo> infos = GoodInfoDao::GetInstance()->ReadFullTableData();
////    for(GoodInfo info : infos)
////    {
////        info.PrintData("_ReadFullTableData");
////    }
//    qDebug() << QString("_ReadFullTableData  size: %1").arg(QString::number(infos.size()));

//}

//// Delete
//void MainWindow::on_pushButton_5_clicked()
//{
//    std::vector<GoodInfo> infos = GoodInfoDao::GetInstance()->Select("id_10055");
//    qDebug() << QString(" __Delete Before %1").arg(QString::number(infos.size()));
//    auto info = infos[0];

//    GoodInfoDao::GetInstance()->Delete(info);

//    infos = GoodInfoDao::GetInstance()->Select("id_10055");
//    qDebug() << QString(" __Delete After %1").arg(QString::number(infos.size()));
//}
